package com.collection;

import java.util.*;

/**
 * @author south wind
 * @version 1.0
 * @date 2025/4/2 19:53
 * @description 去掉List集合中的重复元素
 */
public class Test02_important {
    public static void main(String[] args) {
       List<String> lists = new ArrayList<>();
        lists.add("zhang3");
        lists.add("li4");
        lists.add("wang5");
        lists.add("zhang3");
        lists.add("li4");
        lists.add("qian3");
        //hashSet去重 TODO==无序
        Set<String> set = new HashSet<>(lists);
        List<String> list = new ArrayList<>(set);

        System.out.println(list);

        //LinkedHashSet自动去重  TODO==有序
        Set<String> linkedHashSet = new LinkedHashSet<>(lists);
        List<String> list1 = new ArrayList<>(linkedHashSet);

        System.out.println(list1);

        //TreeSet去重  TODO==无序
        Set<String> treeSet = new TreeSet<>(lists);
        List<String> list2 = new ArrayList<>(treeSet);

        System.out.println(list2);

        //手动实现去重 TODO==无序
        List<String> list3 = new ArrayList<>();
        for (String s : lists) {
            if(!list3.contains(s)){
                list3.add(s);
            }
        }
    }
}
